package ilog

import (
	"github.com/rs/zerolog"
	"gopkg.in/natefinch/lumberjack.v2"
	"io"
	"os"
	"path"
)

func NewLog(conf *Conf) (l zerolog.Logger, e error) {
	level, err := zerolog.ParseLevel(conf.Level)
	if err != nil {
		e = err
		return
	}

	lj := &lumberjack.Logger{
		Filename:   path.Join(conf.Path, conf.Name),
		MaxSize:    conf.MaxSize,
		MaxAge:     conf.MaxAge,
		MaxBackups: conf.MaxBackups,
		LocalTime:  conf.LocalTime,
		Compress:   conf.Compress,
	}

	var w io.Writer
	zerolog.TimeFieldFormat = conf.TimeFormat
	if conf.Console {
		consoleWrite := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: conf.TimeFormat}
		w = zerolog.MultiLevelWriter(consoleWrite, lj)
	} else {
		w = lj
	}

	l = zerolog.New(w).Level(level)

	return
}
